package com.wxproject.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.publicapi.AccessTokenApi;
import com.util.APIStart;
import com.util.ApiResult;
import com.util.HttpUtils;

/**
 * @author wangkai 公众号客服管理
 */
public class NickUserApi {

	private static String addUrl = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=";
	private static String updateUrl = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=";
	private static String delUrl = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=";
	private static String getkflistUrl = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=";
	
	/**
	 * 添加客服帐号（每个公众号最多添加10个客服账号）
	 * 
	 * @param jsonStr
	 *            { "kf_account" : "test1@test",     "nickname" : "客服1",     
	 *            "password" : "pswmd5",}
	 * @return
	 */
	public static ApiResult addNickUser(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(addUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			Integer errcode = jsonObject.getIntValue("errcode");
			String errmsg = jsonObject.getString("errmsg");
			if (errcode.intValue() == 0 && errmsg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "添加成功！");
			}
		}
		return new ApiResult(APIStart.S01, "添加失败！", result);
	}

	/**
	 * 修改客服账号
	 * 
	 * @param jsonStr
	 *            {     "kf_account" : "test1@test",     "nickname" : "客服1",    
	 *             "password" : "pswmd5",}
	 * @return
	 */
	public static ApiResult updateNickUser(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(updateUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			Integer errcode = jsonObject.getIntValue("errcode");
			String errmsg = jsonObject.getString("errmsg");
			if (errcode.intValue() == 0 && errmsg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "修改成功！");
			}
		}
		return new ApiResult(APIStart.S01, "修改失败！", result);
	}

	/**
	 * 修改客服账号
	 * 
	 * @param jsonStr
	 *            {     "kf_account" : "test1@test",     "nickname" : "客服1",    
	 *             "password" : "pswmd5",}
	 * @return
	 */
	public static ApiResult delNickUser(String jsonStr) {
		String result = null;
		try {
			result = HttpUtils.POSTHTTPS(delUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (result != null) {
			JSONObject jsonObject = JSON.parseObject(result);
			Integer errcode = jsonObject.getIntValue("errcode");
			String errmsg = jsonObject.getString("errmsg");
			if (errcode.intValue() == 0 && errmsg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "刪除成功！");
			}
		}
		return new ApiResult(APIStart.S01, "刪除失败！", result);
	}

	/**
	 * 获取所有客服账号
	 * 
	 * @return
	 */
	public static ApiResult getKfList() {
		String result = null;
		try {
			result = HttpUtils.GETHTTPS(getkflistUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		return new ApiResult(APIStart.S00, "获取成功！", result);
	}

	

}
